<?php

namespace App\Modules\Health\Http\Controllers\Admin;

use App\Modules\Health\Repositories\Acupoint;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Arr;

class AcupointController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Acupoint(['body', 'body_point', 'meridian']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('name');
            $grid->column('pin_yin');
            $grid->column('body.name', '部位');
            $grid->column('body_point.name', '部位节点');
            $grid->column('meridian.name', '经脉');
            $grid->column('img')->image(' ', 50, 50);;
            $grid->column('point_img')->image(' ', 50, 50);;
            $grid->column('video_file')->display(function ($video_file){
                return $video_file ? '<a style="margin-right:5px" href="' . $video_file . '" target="_blank">查看</a>' : '';
            });
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->like('name')->width(3);
                $filter->equal('body.id', '部位')->select(
                    \App\Modules\Health\Models\Body::orderByDesc('id')->pluck('name', 'id')->all()
                )->width(3);
                $filter->equal('body_point.id', '部位节点')->select(
                    \App\Modules\Health\Models\BodyPart::orderByDesc('id')->pluck('name', 'id')->all()
                )->width(3);
                $filter->equal('meridian.id', '经脉')->select(
                    \App\Modules\Health\Models\Meridian::orderByDesc('id')->pluck('name', 'id')->all()
                )->width(3);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Acupoint(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('description');
            $show->field('pin_yin');
            $show->field('point_description');
            $show->field('img');
            $show->field('point_img');
            $show->field('video_file');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Acupoint(['body', 'body_point', 'meridian']), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->editor('description')->required();
            $form->text('pin_yin')->required();

            $form->select('body.id', '部位')->required()->options(\App\Modules\Health\Models\Body::orderByDesc('id')->pluck('name', 'id')->all());
            $form->select('body_point.id', '部位节点')->required()->options(\App\Modules\Health\Models\BodyPart::orderByDesc('id')->pluck('name', 'id')->all());
            $form->select('meridian.id', '经脉')->required()->options( \App\Modules\Health\Models\Meridian::orderByDesc('id')->pluck('name', 'id')->all());
            $form->select('wushu_id')->options(\App\Modules\Health\Models\HealthWushuAcupoint::orderByDesc('id')->pluck('name', 'id')->all())->saving(function ($v){
                return (int) $v;
            });
            $form->select('teding_id')->options(\App\Modules\Health\Models\HealthTedingAcupoint::orderByDesc('id')->pluck('name', 'id')->all())->saving(function ($v){
                return (int) $v;
            });

            $form->editor('point_description')->required();
            $form->image('img')->autoUpload()->saveFullUrl()->uniqueName();
            $form->image('point_img')->autoUpload()->saveFullUrl()->uniqueName();
            $form->file('video_file')->autoUpload()->saveFullUrl()->uniqueName();

            $form->saved(function (Form $form, $result){
                if ($result) {
                    $acuId = $form->isCreating() ? $result : $form->getKey();

                    $where = ['acu_id' => $acuId];
                    if ($bodyId = $form->input('body')['id'] ?? '') {
                        \App\Modules\Health\Models\AcuBody::updateOrInsert($where, array_merge($where, ['body_id' => $bodyId]));
                    }
                    if ($bodyPointId = $form->input('body_point')['id'] ?? '') {
                        \App\Modules\Health\Models\AcuBodyPart::updateOrInsert($where, array_merge($where, ['body_part_id' => $bodyPointId]));
                    }
                    if ($meridianId = $form->input('meridian')['id'] ?? '') {
                        \App\Modules\Health\Models\AcuMeridian::updateOrInsert($where, array_merge($where, ['meridian_id' => $meridianId]));
                    }
                }
            });

            $form->deleted(function (Form $form, $result) {
                if ($result) {
                    $ids = Arr::pluck($form->model()->toArray(), 'id');
                    \App\Modules\Health\Models\AcuBody::whereIn('acu_id', $ids)->delete();
                    \App\Modules\Health\Models\AcuBodyPart::whereIn('acu_id', $ids)->delete();
                    \App\Modules\Health\Models\AcuMeridian::whereIn('acu_id', $ids)->delete();
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
